package jp.ne.hatena.d.digitalsoul.warikan.pages

import java.util.ArrayList

import org.apache.wicket.markup.html._
import org.apache.wicket.markup.html.basic._
import org.apache.wicket.markup.html.form._
import org.apache.wicket.markup.html.list._
import org.apache.wicket.model._

import jp.ne.hatena.d.digitalsoul.warikan.actions._
import jp.ne.hatena.d.digitalsoul.warikan.pages.forms._
import jp.ne.hatena.d.digitalsoul.warikan.pages.items._

class SlopeDetailPage extends WebPage {
	
	val action = new SlopeDetailAction
	
	val mainFormModel = new SlopeDetailMainForm
	
	val weightListItems = new ArrayList[WeightListItem]
	
	val roleValues = action.createRoleValues
	
	val weightValues = new ArrayList[String]
	for (val i <- 1 to 20) {
		weightValues.add(0, i.toString)
	}
	
	// form:main
	val main = new Form("main", new CompoundPropertyModel(mainFormModel))
	add(main)
	
	// text:slopeName
	main.add(new TextField("slopeName"))
	
	// button:newLine
	val newLineButton = new Button("newLineButton") {
		override def onSubmit = {
			weightListItems.add(new WeightListItem)
		}
	}
	main.add(newLineButton)
	
	// table:weights
	val weights = new ListView[WeightListItem]("weights", weightListItems) {
		override def populateItem(listItem:ListItem[WeightListItem]) = {
			listItem.setModel(new CompoundPropertyModel(listItem.getModelObject))
			listItem.add(new DropDownChoice("role", roleValues))
			listItem.add(new DropDownChoice("weight", weightValues))
		}
	}
	main.add(weights)
	
	// button:commit
	val commitButton = new Button("commitButton") {
		override def onSubmit = {
			action.executePreparation(mainFormModel, weightListItems)
			setResponsePage(new SlopeListPage)
		}
	}
	main.add(commitButton)
	
	// button:cancel
	val cancelButton = new Button("cancelButton") {
		override def onSubmit = {
			setResponsePage(new SlopeListPage)
		}
	}
	main.add(cancelButton)

}